package com.mrlv.common.wrapper;

import cn.hutool.core.util.StrUtil;
import com.mrlv.common.enums.ResultCodeEnum;
import com.mrlv.common.i18n.LocaleUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;


/**
 * 返回结果封装
 * @author Mrlv
 * @date 2021年12月19日 11:42
 */
@Data
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(description="响应对象")
public class ResultMsg<T> {

	@ApiModelProperty(value = "状态码")
	private int code;

	@ApiModelProperty(value = "描述信息")
	private String msg;

	@ApiModelProperty(value = "返回数据")
	private T data;

	public ResultMsg(ResultCodeEnum status, T data)  {
		String i18nMsg = LocaleUtils.getMessage(status.name());
		this.code = status.getCode();
		this.msg = StrUtil.isNotBlank(i18nMsg) ? i18nMsg : status.getMsg();
		this.data = data;
	}

	public static <T> ResultMsg<T> createByResultStatusCode(ResultCodeEnum status){
		return new ResultMsg<T>(status.getCode(), status.getMsg(), null);
	}
	public static <T> ResultMsg<T> createByResultStatusCode(ResultCodeEnum status, T data){
		return new ResultMsg<T>(status.getCode(), status.getMsg(), data);
	}
	public static <T> ResultMsg<T> createBySuccess(){
		return new  ResultMsg<T>(ResultCodeEnum.OK.getCode(), null, null);
	}
	public static <T> ResultMsg<T> createBySuccessMessage(String msg){
		return new ResultMsg<T>(ResultCodeEnum.OK.getCode(), msg, null);
	}
	public static <T> ResultMsg<T> createBySuccess(T data){
		return new ResultMsg<T>(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getMsg(), data);
	}
	public static <T> ResultMsg<T> createBySuccess(String msg, T data){
		return new ResultMsg<T>(ResultCodeEnum.OK.getCode(), msg, data);
	}

	public static <T> ResultMsg<T> createResult(ResultCodeEnum status, T data){
		return new ResultMsg<T>(status, data);
	}
	public static <T> ResultMsg<T> createResult(int code, String message, T data){
		return new ResultMsg<T>(code, message, data);
	}
	public static <T> ResultMsg<T> createResult(int code,  T data){
		return new ResultMsg<T>(code, null, data);
	}
	public static <T> ResultMsg<T> createResult(ResultCodeEnum status){
		return new ResultMsg<T>(status, null);
	}


	public static <T> ResultMsg<T> createByError(){
		return new ResultMsg<T>(ResultCodeEnum.SYSTEM_ERR.getCode(), ResultCodeEnum.SYSTEM_ERR.getMsg(), null);
	}

	public static <T> ResultMsg<T> createByErrorMessage(String errorMessage){
		return new ResultMsg<T>(ResultCodeEnum.SYSTEM_ERR.getCode(), errorMessage, null);
	}

	public static <T> ResultMsg<T> createByErrorCodeMessage(int errorCode, String errorMessage){
		return new ResultMsg<T>(errorCode, errorMessage, null);
	}

	public static <T> ResultMsg<T> createByErrorCodeMessageData(int errorCode, String errorMessage, T data){
		return new ResultMsg<T>(errorCode, errorMessage, data);
	}

	public static <T> ResultMsg<T> createByInvalidParam(){
		return new ResultMsg<T>(ResultCodeEnum.INVALID_PARAM.getCode(), ResultCodeEnum.INVALID_PARAM.getMsg(), null);
	}


}
